﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slja.Model;
using Slja.DAL;
using AjaxPro.Services;
using System.Data;
using System.Text.RegularExpressions;


namespace Slja.BLL
{
    public class BLLCommon
    {
        internal static readonly int _PageSize = 10;
       [AjaxPro.AjaxMethod]
       public static string  GetCityFromKeyWord(string keyword)
        {
            string strXml = "";
            if (keyword.Length >= 2)
            {
                Regex regChz2 = new Regex(@"[\u0391-\uFFE5]{2,}");//2个以上的汉字
                Regex regEng2 = new Regex(@"[A-Z,a-z]{2,}");//2个以上的拼音字母

                string strWhere = "1=0";
                if (regChz2.IsMatch(keyword))
                {
                    strWhere = " f_title like '" + keyword + "%'";
                }
                if (regEng2.IsMatch(keyword))
                {
                    strWhere = " f_pyshort like '" + keyword + "%' OR f_pinyin like '"+keyword+"%'";
                }

                RegionCity dal = new RegionCity();
                DataSet ds = dal.GetList(strWhere);
                if (ds.Tables.Count > 0)
                {
                    strXml = BuildXmlData(ds.Tables[0], "fid", "f_title", "{0}*{1}", "{1}");
                }
            }
            return strXml;
        }     
     
       public static string BuildXmlData(DataTable dtSource, string valueField, string textField, string valueFmt, string textFmt)
       {
           return BuildXmlData(dtSource, valueField, textField, valueFmt, textFmt, 1);
       
       }
        /// <summary>
        /// 输出取到的数据为XML文本
        /// </summary>
        /// <param name="dtSource"></param>
        /// <param name="valueField"></param>
        /// <param name="textField"></param>
        /// <param name="valueFmt"></param>
        /// <param name="textFmt"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
       public static string BuildXmlData(DataTable dtSource, string valueField, string textField, string valueFmt, string textFmt,int pageCount)
       {
           StringBuilder sb = new StringBuilder();
           sb.Append("<?xml version='1.0' encoding='utf-8'?><all pagecount='"+pageCount.ToString()+"'>");

           DataTable dt = dtSource;
           string na, id;
           na = ""; id = "";
           for (int i = 0; i < dt.Rows.Count; i++)
           {
               id = dt.Rows[i][valueField].ToString();
               na = dt.Rows[i][textField].ToString();
               sb.AppendFormat("<response value='" + valueFmt + "'>" + textFmt + "</response>", id, na);
           }

           sb.Append("</all>");
           return sb.ToString();
       }
       public static string ObjIDFromSpliting(string strIDName)
       {
           String[] arr = strIDName.Split('*');
           return arr[0];
       }
       public static string ObjNameFromSpliting(string strIDName)
       {
           String[] arr = strIDName.Split('*');
           return arr[1];
       }
       public static string GetShopCode(int city, string ShopName,out string TLCode)
       {
           TLCode = "";
           return "";
       }
    }
}
